package com.ibk.system;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.Browser;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.ibk.system.map.GoogleMapTest;

public class WebViewActivity extends Activity {
	public WebView web;

	// public Button bt;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview_layout);
		web = (WebView) findViewById(R.id.webview);
		// bt = (Button) findViewById(R.id.bt);
		// bt.setOnClickListener(new OnClickListener() {
		// public void onClick(View v) {
		// web.loadUrl("javascript:fromApp()");
		// }
		// });

		String url = getIntent().getStringExtra("url");
		if (url == null) {
			url = "http://192.168.0.74:8080/simple-webapp";
		}
		// 자바스크립트를 사용할 수 있도록 설정
		WebSettings setting = web.getSettings();
		setting.setJavaScriptEnabled(true);
		setting.setBuiltInZoomControls(true);
		// 자바스크립트에서는 허용된 것만 호출할 수 있다.
		// 이미 정의되어 있는 자바스크립트에서 호출할 메소드를 갖고 있는 객체를 등록시키는 작업
		web.addJavascriptInterface(new JavascriptInterfaceTest(), "android");
		// web.loadUrl("http://www.google.co.kr");
		final Activity activity = this;

		web.setWebViewClient(new WebViewClient() {
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				Toast.makeText(activity, "Oh no! " + description,
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				Log.w("page finished", "url : " + url);
				if (url.startsWith("sms:")) {
					Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
					startActivity(i);
				}
				if (url.startsWith("tel:")) {
					Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(url));
					startActivity(i);
				}
				if (url.startsWith("mailto:")) {
					Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
					startActivity(i);
				}
				if (url.startsWith("map://")) {
					Intent i = new Intent(getApplicationContext(),
							GoogleMapTest.class);
					startActivity(i);
				}
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				Log.w("page started", "url : " + url);
				if (url.startsWith("sms:")) {
					Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
					startActivity(i);
				}
				if (url.startsWith("tel:")) {
					Intent i = new Intent(Intent.ACTION_CALL, Uri.parse(url));
					startActivity(i);
				}
				if (url.startsWith("mailto:")) {
					Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
					startActivity(i);
				}
				if (url.startsWith("map://")) {
					Intent i = new Intent(getApplicationContext(),
							GoogleMapTest.class);
					startActivity(i);
				}
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view,
					String overrideUrl) {
				Log.w("override Url call", "url : " + overrideUrl);
				if (overrideUrl.startsWith("http://")) {
					return false;
				} else {
					boolean override = false;
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse(overrideUrl));
					intent.addCategory(Intent.CATEGORY_BROWSABLE);
					intent.putExtra(Browser.EXTRA_APPLICATION_ID,
							getPackageName());
					if (overrideUrl.startsWith("sms:")) {
						Intent i = new Intent(Intent.ACTION_SENDTO, Uri
								.parse(overrideUrl));
						startActivity(i);
						return true;
					}
					if (overrideUrl.startsWith("tel:")) {
						Intent i = new Intent(Intent.ACTION_CALL, Uri
								.parse(overrideUrl));
						startActivity(i);
						return true;
					}
					if (overrideUrl.startsWith("mailto:")) {
						Intent i = new Intent(Intent.ACTION_SENDTO, Uri
								.parse(overrideUrl));
						startActivity(i);
						return true;
					}
					if (overrideUrl.startsWith("map://")) {
						Intent i = new Intent(getApplicationContext(),
								GoogleMapTest.class);
						startActivity(i);
						return true;
					}
					try {
						startActivity(intent);
						override = true;
					} catch (ActivityNotFoundException ex) {
					}
					return override;
				}
			}
		});
		web.loadUrl(url);

	}

	// 자바스크립트로 호출 가능한 java의 메소드를 가지고 있는 class
	public class JavascriptInterfaceTest {
		public void vibration() {
			// 시스템에서 서비스를 제공해주고 있는 진동과 관련된 객체를 얻어와야 한다.
			// 이러한 객체는 주로 SystemService를 통해 얻어올 수 있다.
			// 실제 안드로이드 프레임워크 내부에서 동작하는 객체를 얻어올때 사용하는 것이 SystemService임
			Vibrator vib = (Vibrator) WebViewActivity.this
					.getSystemService(Context.VIBRATOR_SERVICE);
			vib.vibrate(500);
		}
	}
}
